#ifndef __TASK_H__
#define __TASK_H__

#include <memory>
#include "BaseTask.h"
#include "ConcreteTask.h"
#include "HFuncType.h"
#include "HTask.h"
#include "ThreadSafePrinter.h"
#include "UtilsDefine.h"

HTHREADPOOL_NAMESPACE_BEGIN

    class Task : public HTaskObject
    {
    public:
        Task() = default;

        template<typename FUNC>
        Task(FUNC&& func, int priority = 0)
        : m_impl(new ConcreteTask<FUNC>(std::forward<FUNC>(func)))
        , m_priority(priority)
        {
        }

        Task(const Task& other) = delete;
        Task& operator=(const Task& other) = delete;

        Task(Task&& task) noexcept
        : m_impl(std::move(task.m_impl))
        , m_priority(task.m_priority)
        {
            task.m_impl = nullptr;
        }

        Task& operator=(Task&& task) noexcept
        {
            if(this == &task) {
                return *this;
            }

            m_impl      = std::move(task.m_impl);
            task.m_impl = nullptr;
            m_priority  = task.m_priority;
            return *this;
        }

        void operator()()
        {
            if(likely(m_impl)) {
                m_impl->execute();
            }
        }

        bool operator>(const Task& task) const { return m_priority > task.m_priority; }

        bool operator<(const Task& task) const { return m_priority < task.m_priority; }

    private:
        std::unique_ptr<AbstractTask> m_impl;
        int                           m_priority;  //任务优先级
    };

    // 由于 Task 类现在带有模板参数，需要更新类型别名
    using TaskRef    = Task&;
    using TaskPtr    = Task*;
    using TaskArr    = std::vector<Task>;
    using TaskArrRef = std::vector<Task>&;

HTHREADPOOL_NAMESPACE_END

#endif
